package com.xyz.graph.util;

import java.util.*;

public class CollectionUtil {

    /**
     * 将list结果放入set集合
     * @param paths
     * @return
     */
    public static HashMap<String,ArrayList<HashMap<String,String>>> list2Set(List<ArrayList<HashMap<String,String>>> paths){
        Set<HashMap<String,String>> entitySet=new HashSet<>();
        Set<HashMap<String,String>> relationSet=new HashSet<>();
        HashMap<String,ArrayList<HashMap<String,String>>>  result=new HashMap<>();

        if(paths !=null){
            for(ArrayList<HashMap<String,String>> path:paths){
                for(HashMap<String,String> element:path){
                    if(element.get("parentType").equals("entity")){
                        entitySet.add(element);
                    }else{
                        relationSet.add(element);
                    }
                }
            }
        }

        ArrayList<HashMap<String,String>> entityList=set2Array(entitySet);
        ArrayList<HashMap<String,String>> relationList=set2Array(relationSet);
        result.put("entity",entityList);
        result.put("relation",relationList);
        //保证输入paths为空，result中也会有entity和relation的Key
        return result;
    }

    /**
     * 将set结果放入list集合
     * @param set
     * @return
     */
    public static ArrayList<HashMap<String,String>> set2Array(Set<HashMap<String,String>> set){
        ArrayList<HashMap<String,String>> list=new ArrayList<>();
        Iterator<HashMap<String,String>> iterator=set.iterator();
        while (iterator.hasNext()){
            list.add(iterator.next());
        }
        return list;
    }

}
